package testrank.predictors;

import java.util.HashSet;
import java.util.Set;

import testrank.ElementInfo;
import testrank.TestManager;
import testrank.TestRankConstants;
import testrank.affinity.WordGroup;

/**
 * @author Hagai Cibulski
 *
 */
public class AffinityContext {
	private static Set<String> warnings = new HashSet<String>();
	
	public Set<WordGroup> allCoveringTestsOfMethodWordGroups ;
	public Set<WordGroup> allCoveredMethodsOfTestWordGroups;

	public AffinityContext(ExecutionCountPredictor countPredictor, String methodSignature, String testSignature) {
		if (TestRankConstants.AFFINITY_ALT_NORMALIZATION != TestRankConstants.AffinityNormalization.ByTfIdfContext)
			return;
		
		// find all Covering Tests Of Method
		allCoveringTestsOfMethodWordGroups = new HashSet<WordGroup>();
		Set<String> allCoveringTestsOfMethod = countPredictor.getCoveringTests(methodSignature);
		for (String t : allCoveringTestsOfMethod) {
			ElementInfo info = TestManager.testLocator().getCodeElementBySignature(t);
			if (info == null) {
				String warn = "WARN: no getCodeElementBySignature for test: " + t;
				if (warnings.add(warn)) {
					System.out.println(warn);
				}
				continue;
			}
			WordGroup g = info.getWordGroup();
			if (g != null) {
				allCoveringTestsOfMethodWordGroups.add(g);
			}
		}
		
		// find all Covered Methods Of Test 
		allCoveredMethodsOfTestWordGroups = new HashSet<WordGroup>();
		Set<String> allCoveredMethodsOfTest= countPredictor.getCoveredMethods(testSignature);
		for (String m : allCoveredMethodsOfTest) {
			ElementInfo info = TestManager.locator().getCodeElementBySignature(m); 
			if (info == null) {
				String warn = "WARN: no getCodeElementBySignature for method: " + m;
				if (warnings.add(warn)) {
					System.out.println(warn);
				}
				continue;
			}
			WordGroup g = info.getWordGroup();
			if (g != null) {
				allCoveredMethodsOfTestWordGroups.add(g);
			}
		}
	}

}
